package com.smartleanx.module.hrm.service.examine.examineFlowPath;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.smartleanx.module.hrm.controller.admin.examine.examineFlowPath.vo.*;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineFlowPath.ExamineFlowPathDO;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.util.object.BeanUtils;

import com.smartleanx.module.hrm.dal.mysql.examine.examineFlowPath.ExamineFlowPathMapper;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.*;

/**
 * 考核流程 Service 实现类
 *
 * @author 
 */
@Service
@Validated
public class ExamineFlowPathServiceImpl implements ExamineFlowPathService {

    @Resource
    private ExamineFlowPathMapper examineFlowPathMapper;

    @Override
    public Integer createExamineFlowPath(ExamineFlowPathSaveReqVO createReqVO) {
        // 插入
        ExamineFlowPathDO examineFlowPath = BeanUtils.toBean(createReqVO, ExamineFlowPathDO.class);
        examineFlowPathMapper.insert(examineFlowPath);
        // 返回
        return examineFlowPath.getId();
    }

    @Override
    public void updateExamineFlowPath(ExamineFlowPathSaveReqVO updateReqVO) {
        // 校验存在
        validateExamineFlowPathExists(updateReqVO.getId());
        // 更新
        ExamineFlowPathDO updateObj = BeanUtils.toBean(updateReqVO, ExamineFlowPathDO.class);
        examineFlowPathMapper.updateById(updateObj);
    }

    @Override
    public void deleteExamineFlowPath(Integer id) {
        // 校验存在
        validateExamineFlowPathExists(id);
        // 删除
        examineFlowPathMapper.deleteById(id);
    }

    private void validateExamineFlowPathExists(Integer id) {
        if (examineFlowPathMapper.selectById(id) == null) {
            throw exception(EXAMINE_FLOW_PATH_NOT_EXISTS);
        }
    }

    @Override
    public ExamineFlowPathDO getExamineFlowPath(Integer id) {
        return examineFlowPathMapper.selectById(id);
    }

    @Override
    public PageResult<ExamineFlowPathDO> getExamineFlowPathPage(ExamineFlowPathPageReqVO pageReqVO) {
        return examineFlowPathMapper.selectPage(pageReqVO);
    }

}